package com.wn.icloud.provider.uac.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wn.icloud.biz.BaseBiz;
import com.wn.icloud.common.context.BaseContextHandler;
import com.wn.icloud.common.msg.TableResultResponse;
import com.wn.icloud.provider.uac.entity.Cover;
import com.wn.icloud.provider.uac.mapper.CoverMapper;
import com.wn.icloud.provider.uac.service.dto.CoverDTO;
import com.wn.icloud.provider.uac.vo.CoverVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service("coverBiz")
@Transactional(rollbackFor = Exception.class)
public class CoverBiz extends BaseBiz<CoverMapper, Cover> {

    /**
     * 查询所有数据
     * @return
     */
    public TableResultResponse<CoverVO> selectCoverAll(CoverDTO dto) {
        Example example = new Example(Cover.class);
        if (StringUtils.isNotBlank(dto.getName())) {
            example.createCriteria().andLike("name", "%" + dto.getName() + "%");
        }
        if (StringUtils.isNotBlank(dto.getStatus())) {
            example.createCriteria().andEqualTo("status", dto.getStatus());
        }
        example.setOrderByClause("order_num asc,publish_time desc");
        Page<Cover> result = PageHelper.startPage(dto.getPage(), dto.getSize());
        List<Cover> list = mapper.selectByExample(example);
        List<CoverVO> listData= new ArrayList<CoverVO>();
        for (Cover cover : list) {
            CoverVO coverVO = new CoverVO();
            coverVO.setId(cover.getId());
            coverVO.setName(cover.getName());
            coverVO.setOrderNum(cover.getOrderNum());
            coverVO.setStatus(cover.getStatus());
            coverVO.setPublishPersonal(cover.getPublishPersonal());
            coverVO.setPublishTime(cover.getPublishTime());
            coverVO.setPicture(cover.getPicture());
            listData.add(coverVO);
        }
        return new TableResultResponse<CoverVO>(result.getTotal(), listData);
    }

    /**
     * 增加
     * @param coverDTO
     */
    public void addCover(CoverDTO coverDTO) {
        Cover cover = getCover(coverDTO);
        mapper.insertSelective(cover);
    }

    /**
     * 编辑
     * @param coverDTO
     */
    public void updateCover(CoverDTO coverDTO) {
        Cover cover = getCover(coverDTO);
        mapper.updateByPrimaryKey(cover);
    }

    /**
     * 添加编辑
     * @param coverDTO
     * @return
     */
    public Cover getCover(CoverDTO coverDTO) {
        Cover cover = new Cover();
        cover.setId(coverDTO.getId());
        cover.setName(coverDTO.getName());
        cover.setStatus(coverDTO.getStatus());
        cover.setOrderNum(coverDTO.getOrderNum());
        cover.setPublishTime(new Date());
        cover.setPublishPersonal(BaseContextHandler.getUsername());
        cover.setPicture(coverDTO.getPicture());
        return cover;
    }

    public void deleteCover(String ids) {
        String[] str = ids.split(",");
        mapper.deleteCoverByIds(str);
    }
}
